package com.cloud.feeling.ststem.controller;

import java.util.ArrayList;
import java.util.List;

import javax.swing.Spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.common.RestCode;
import com.cloud.common.RestResponse;
import com.cloud.common.model.Depart;
import com.cloud.common.model.Fruits;
import com.cloud.common.model.TreeModel;
import com.cloud.common.utils.TreeGridResult;
import com.cloud.feeling.ststem.service.DepartService;
import com.google.common.collect.Lists;

/**
 * 
  * @Description: 部门组织管理
  * @author: feelj
  * @date: 2018年10月1日 上午9:38:04 
  *
 */
@Controller
@RequestMapping
public class DepartController {
	
	@Autowired
	public DepartService departService;
	
	@GetMapping("/depart")
	public String depart() {
		return "depart/departlist";
	}
	
	@PostMapping("/departlist")
	@ResponseBody
	public TreeGridResult<Depart> departlist(){
		List<Depart> list = departService.list();
		TreeGridResult<Depart> result=new TreeGridResult<>(list.size(), list);
		return result;
	}
	
	@PostMapping("/treegridlist")
	@ResponseBody
	public TreeGridResult<Fruits> TreeGrid(){
		TreeGridResult<Fruits> result=new TreeGridResult<>();
		
		result.setCode(0);
		result.setMsg("测试数据");
		result.setIs(false);
	    result.setCount(924);
	    result.setTip("操作成功");
		result.setData(Lists.newArrayList(
				new Fruits(1,0,"水果"),
				new Fruits(101,1,"香蕉"),
				new Fruits(103,1,"梨"),
				new Fruits(10101,101,"红富士苹果"),
				new Fruits(10102,101,"红富士苹果"),
				new Fruits(102,1,"苹果")
				));
	    
		return result;
	}
	
	@GetMapping("addDepart")
	public String addDepart(Integer parentId,Model model) {
		if(parentId!=0) {
			Depart depart = departService.getByparentId(parentId);
			model.addAttribute("depart", depart);
		}else {
			Depart depart=new Depart();
			depart.setDepartName("无");
			depart.setId(0);
			model.addAttribute("depart", depart);
		}
		return "/depart/adddepart";
	}
	
	@GetMapping("/showprentdepart")
	public String showprentdepart() {
		
		return "/common/showdepart";
	}
	
	@GetMapping("updateDepart")
	public String updateDepart(Depart depart,Model model) {
		depart = departService.getByparentId(depart.getId());
		model.addAttribute("depart",depart);
		if(depart!=null&&depart.getParentId()!=0) {
			Depart parentdepart = departService.getByparentId(depart.getParentId());
			model.addAttribute("parentName",parentdepart.getDepartName());
		}else {
			model.addAttribute("parentName","无");
		}
		return "/depart/editdepart";
	}
	
	@ResponseBody
	@RequestMapping("/saveDepart")
	public RestResponse<RestCode> saveDepart(Depart depart){
		departService.savedepart(depart);
		return RestResponse.success();
	}
	
	@GetMapping("/deleteDepartByid")
	@ResponseBody
	public RestResponse<RestCode> deleteDepartByid(Integer id){
		departService.deleteDepartByid(id);
		return RestResponse.success();
	}
	
	@PostMapping("/selecttree")
	@ResponseBody
	public List<TreeModel> selecttree(){
		TreeModel t=new TreeModel();
		t.setId(2);
		t.setLv("1");
		t.setN("asfdfg");
		TreeModel t2=new TreeModel();
		t2.setId(1);
		t2.setLv("2");
		t2.setN("asfddd");
		TreeModel t3=new TreeModel();
		t3.setId(1);
		t3.setLv("3");
		t3.setN("asfdv");
		List<TreeModel> res=new ArrayList<>();
		res.add(t);
		res.add(t2);
		res.add(t3);
		return res;
	}
	
	@PostMapping("/selectDepartTree")
	@ResponseBody
	public List<Depart> selectDepartTree() {
		List<Depart> list = departService.list();
		return list;
	}
	

}
